<template>
    <div>
      <!-- 面包屑导航 -->
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>活动管理</el-breadcrumb-item>
        <el-breadcrumb-item>活动列表</el-breadcrumb-item>
        <el-breadcrumb-item>活动详情</el-breadcrumb-item>
      </el-breadcrumb>
      <!-- 卡片区域 -->
      <el-card>
        <!-- 添加角色区域 -->
        <el-row>
          <el-col>
            <el-button type="primary">添加角色</el-button>
          </el-col>
        </el-row>
        <!-- 角色列表区域 -->
        <el-table :data="rolesList" border stripe>
          <el-table-column type="expand">
            <template slot-scope="scope">
              <!-- 栅格布局 -->
              <el-row :class="[bdbottom, i1 ===0 ? 'bdtop' : '', vcenter]" v-for="(item1, i1) in scope.row.children" :key="item1.id">
                <el-col :span="5">
                  <el-tag >{{item1.authName}}</el-tag>
                  <i class="el-icon-caret-right"></i>
                </el-col>
                <el-col :span="19">
                  <el-row :class="[i2 ===0? '' : 'bdtop', vcenter]" v-for="(item2, i2) in item1.children" :key="item2.id">
                    <el-col :span="6">
                      <el-tag type="success" >{{item2.authName}}</el-tag>
                      <i class="el-icon-caret-right"></i>
                    </el-col>
                    <el-col :span="18">
                      <el-tag type="warning" v-for="item3 in item2.children" :key="item3.id" closable @close="removeRightByid()">{{item3.authName}}</el-tag>
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
            </template>
          </el-table-column>
          <el-table-column type="index"></el-table-column>
          <el-table-column label="角色名称" prop="roleName"></el-table-column>
          <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
          <el-table-column label="操作" width="300px">
            <template slot-scope="scope">
              <el-button size="mini" type="primary" icon="el-icon-edit" >编辑</el-button>
              <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
              <el-button size="mini" type="warning" icon="el-icon-setting" @click="showSetRightDialog(scope.row)">分配权限</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>

      <!-- 分配权限显示对话框 -->
      <el-dialog
        title="分配权限"
        :visible.sync="setRightDialogVisible"
        width="50%" @click="setRightDialogClosed" >
        <!-- 树形空间 -->
        <el-tree 
          :data="rightsList" 
          :props="treeProps" 
          show-checkbox 
          node-key="id" 
          default-expand-all
          :default-expanded-keys="defkeys">
        </el-tree>
        <span slot="footer" class="dialog-footer">
          <el-button @click="setRightDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="setRightDialogVisible = false">确 定</el-button>
        </span>
      </el-dialog>
    </div>
</template>
<script>
// 注意：每个组件需要用div进行包裹，要不然多个标签会报错

  export default {
    name: '',
    data () {
      return {
        rolesList: [],
        //控制分配对话框的显示与隐藏
        setRightDialogVisible: 'false',
        //所有权限的数据
        rightsList: [],
        treeProps: {
          children: 'children',
          label: 'authName'
        },
        defkeys: []//通过路径找到相应的ID值
      }
    },
    created() {
      this.getRolesList()
    },
    methods: {
      async getRolesList() {
        const {data: res} = await this.$http.get('roles')
        if(res.meta.status !== 200) {
          return this.$message.error('获取列表失败')
        }
        this.rolesList = res.data
      },
      // 展示分配权限的对话框
      async showSetRightDialog(role) {
        //获取所有权限的数据
        const {data: res} = await this.$http.get('rights/tree')
        if (res.meta.status !== 200) {
          return this.$sessage.error('获取失败')
        }
        this.rightsList = res.data

        this.getLeafKeys(role, this.defkeys)//获取三级节点的ID

        setRightDialogVisible: true

      },
      //通过递归形式，获取角色下所有的三级权限的ID，并保存到defKeys中
      getLeafKeys(node, arr) {
        if(!node.children) {//如果node没有children属性，则是三级节点
          return arr.push(node.id)
        }
        node.children.forEach(item => {
          this.getLeafKeys(item, arr)
        })
      },
      //监听分配权限对话框的关闭事件
      setRightDialogClosed() {
        this.defkeys = []
      }
    }
  }
</script>
<style  scoped>
  .el-tag {
    margin: 7px;
  }
  .bdtop {
    border-top: 1px solid #eee;
  }
  .bdbottom {
    border-bottom: 1px solid #eee;
  }
  .vcenter {
    display: flex;
    align-items: center;
  }
</style>
